package com.guchenbo.example.redisson;

import org.redisson.Redisson;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

/**
 * @author guchenbo
 * @date 2024/4/15
 */
public class RedissonDemo {
    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        //获取客户端
        RedissonClient redissonClient = Redisson.create(config);
        //获取所有的key
        redissonClient.getKeys().getKeys().forEach(key -> System.out.println(key));

        lock(redissonClient);

        //关闭客户端
        redissonClient.shutdown();
    }

    private static void lock(RedissonClient redissonClient) throws InterruptedException {
        RSemaphore semaphore = redissonClient.getSemaphore("gcb-lock");
        semaphore.addPermits(0);

        boolean await = semaphore.tryAcquire(10, TimeUnit.SECONDS);
        System.out.println(await);

    }
}
